
from datetime import datetime
import time
import os

from apscheduler.schedulers.background import BackgroundScheduler


def tick():
    print('Tick! The time is: %s' % datetime.now())

def write_in_text():
    file = open('test.txt','a')
    file.write('Current time is %s \r' % datetime.now())
    file.flush()
    file.close()

if __name__ == '__main__':
    # 异步 必须 保持主线程正在执行任务
    scheduler = BackgroundScheduler()
    scheduler.add_job(tick, 'cron', minute="*/1")
    scheduler.add_job(write_in_text,'cron',minute="*/1")
    scheduler.start()
    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

    try:
        # This is here to simulate application activity (which keeps the main thread alive).
            while(True):
                time.sleep(5)
                print('ok')
    except (KeyboardInterrupt, SystemExit):
        # Not strictly necessary if daemonic mode is enabled but should be done if possible
        scheduler.shutdown()